#include <lwip/sockets.h>
#include "inc/esp_wifi_apsta.h"
#include "E:\app\Espressif\frameworks\esp-idf-v5.0.1\components\esp_wifi\include\esp_wifi.h"
#include "E:\app\Espressif\frameworks\esp-idf-v5.0.1\components\esp_http_server\include\esp_http_server.h"
// 热点名称 密码  可连接数量
#define AP_WIFI_SSID		"esp32s3"
#define AP_WIFI_PASS		"88888888"
#define AP_MAX_STA_CONN		4

// 路由器wifi名称 密码
#define STA_WIFI_SSID		"a12345"
#define STA_WIFI_PASS		"aa123456"

static const char *TAG = "apsta";

static void wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
	if (event_id == WIFI_EVENT_AP_STACONNECTED) {
		wifi_event_ap_staconnected_t *event = (wifi_event_ap_staconnected_t *)event_data;
		ESP_LOGI(TAG, "station " MACSTR " join, AID=%d",
				MAC2STR(event->mac), event->aid);
	} else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
		wifi_event_ap_stadisconnected_t *event = (wifi_event_ap_stadisconnected_t *)event_data;
		ESP_LOGI(TAG, "station " MACSTR " leave, AID=%d",
				MAC2STR(event->mac), event->aid);
	}
}

static void sta_start_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
	esp_wifi_connect();
}

// static void got_ip_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
// 	//No need to log, wifi driver logs automatically
// 	esp_netif_dns_info_t dns;
// 	if (esp_netif_get_dns_info(_esp_netif_sta, ESP_NETIF_DNS_MAIN, &dns) == ESP_OK) {
// 		dhcps_dns_setserver((const ip_addr_t *)&dns.ip);
// 		ESP_LOGI(TAG, "set dns to:" IPSTR, IP2STR(&dns.ip.u_addr.ip4));
// 	}
// }


static void wifi_init_apsta(void)
{
    esp_netif_create_default_wifi_ap();
	esp_netif_create_default_wifi_sta();

	wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
	ESP_ERROR_CHECK(esp_wifi_init(&cfg));
    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));

	wifi_config_t wifi_config = {
		.ap = {
			.ssid = AP_WIFI_SSID,
			.ssid_len = strlen(AP_WIFI_SSID),
			.password = AP_WIFI_PASS,
			.max_connection = AP_MAX_STA_CONN,
			.authmode = WIFI_AUTH_WPA_WPA2_PSK
		},
	};
	if(strlen(AP_WIFI_PASS) == 0){wifi_config.ap.authmode = WIFI_AUTH_OPEN;}
		
	
	ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));

	wifi_config_t cfg1 = {
		.sta = {
			.ssid = STA_WIFI_SSID,
			.password = STA_WIFI_PASS,
			.threshold = {.rssi=0, .authmode = WIFI_AUTH_WPA_WPA2_PSK},
			.pmf_cfg = {
				.capable = true,
				.required = false
			},
		},
	};
	ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &cfg1));
    ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, 
                                                ESP_EVENT_ANY_ID, 
                                                &wifi_event_handler, 
                                                NULL));
	ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, 
                                                WIFI_EVENT_STA_START, 
                                                sta_start_handler, 
                                                NULL));
	
    ESP_ERROR_CHECK(esp_wifi_start());

	esp_netif_ip_info_t ip_info;
	esp_netif_get_ip_info(esp_netif_get_handle_from_ifkey("WIFI_AP_DEF"), &ip_info);

	char ip_addr[16];
	inet_ntoa_r(ip_info.ip.addr, ip_addr, 16);
	ESP_LOGI(TAG, "Set up softAP with IP: %s", ip_addr);

	ESP_LOGI(TAG, "wifi_init_softap finished. SSID:'%s' password:'%s'", AP_WIFI_SSID, AP_WIFI_PASS);
}


void esp_wifi_mode_apsta(void)
{
	wifi_init_apsta();
}

